﻿using System;
using System.Collections.Generic;
using System.Linq;
using Loki.Engine;
using Luna.Data.Accounting;
using NUnit.Framework;

namespace Luna.Data.Tests
{
    public class AccountingCategoryTest : AssertionHelper
    {
        public INamedEntityData Category1 { get; set; }
        public INamedEntityData Category2 { get; set; }
        public INamedEntityData Category3 { get; set; }

        private bool AreSame(INamedEntityData P_Cat1, INamedEntityData P_Cat2)
        {
            bool b = true;
            b &= P_Cat1.PK == P_Cat2.PK;
            b &= P_Cat1.Name == P_Cat2.Name;
            b &= P_Cat1.Description == P_Cat2.Description;
            return b;
        }

        public IAccountingDataProvider Data { get; set; }

        private INamedEntityData Create(string P_Name)
        {
            INamedEntityData L_Return = Data.Create<INamedEntityData>();
            L_Return.PK = Guid.NewGuid();
            L_Return.Name = P_Name + "_Name";
            L_Return.Description = P_Name + "_Description";
            return L_Return;
        }

        [SetUp]
        public void SetUp()
        {
            Data = ContextHelper.GetDataModel<LunaDataModel>().Accounting;

            Category1 = Create("Category1");
            Category2 = Create("Category2");
            Category3 = Create("Category3");

            CleanTable();
        }

        private void CleanTable()
        {
            IEnumerable<INamedEntityData> L_Categories = Data.GetAllCategories();
            foreach (var L_Cat in L_Categories)
            {
                Data.DeleteCategory(L_Cat.PK);
            }
        }

        [Test]
        public void TestInsertAndGet()
        {
            Data.SaveCategory(Category1);
            INamedEntityData L_Category = Data.GetCategory(Category1.PK);
            Expect(AreSame(Category1, L_Category), Is.True);
        }

        [Test]
        public void TestUpdateAndGet()
        {
            Data.SaveCategory(Category1);
            Category1.Name = "Category1_Name_Modified";
            Data.SaveCategory(Category1);

            INamedEntityData L_Category = Data.GetCategory(Category1.PK);
            Expect(AreSame(Category1, L_Category), Is.True);

            Expect(L_Category.Name, Is.EqualTo("Category1_Name_Modified"));
        }

        [Test]
        public void TestInsertAndGetAll()
        {
            Data.SaveCategory(Category1);
            Data.SaveCategory(Category2);
            Data.SaveCategory(Category3);

            int L_Count = Data.GetAllCategories().Count();
            Expect(L_Count, Is.EqualTo(3));
        }

        [Test]
        public void TestInsertAndDelete()
        {
            Data.SaveCategory(Category1);
            Data.SaveCategory(Category2);
            Data.DeleteCategory(Category1.PK);

            int L_Count = Data.GetAllCategories().Count();
            Expect(L_Count, Is.EqualTo(1));
            INamedEntityData L_Category = Data.GetCategory(Category1.PK);
            Expect(L_Category, Is.Null);

        }
    }
}
